<template>
  <div class="index-board">
    <div class="index-item" v-for="index in store.indexes" :key="index.code">
      <div class="name">{{ index.name }}</div>
      <div class="price" :class="{ up: index.change > 0, down: index.change < 0 }">
        {{ index.price }}
      </div>
      <div :class="{ up: index.change > 0, down: index.change < 0 }">
        {{ index.change }} ({{ index.changePercent }}%)
      </div>
    </div>
  </div>
</template>

<script setup>
import { useStockStore } from '../store'
import { onMounted, onUnmounted } from 'vue'

const store = useStockStore()
let timer = null

const updateIndexData = async () => {
  const codes = store.indexes.map(index => index.code)
  await store.initIndexes()
}

onMounted(async () => {
  // 初始化指数数据
  await updateIndexData()
  
  // 启动定时更新
  timer = setInterval(async () => {
    await updateIndexData()
  }, 5000)
})

onUnmounted(() => {
  // 组件卸载时清除定时器
  if (timer) {
    clearInterval(timer)
  }
})
</script>

<style scoped>
.index-board {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
  padding: 15px;
  background: #f5f5f5;
  border-radius: 8px;
}

.index-item {
  text-align: center;
  padding: 10px;
}

.up {
  color: #f56c6c;
}

.down {
  color: #67c23a;
}

.price {
  font-size: 24px;
  font-weight: bold;
  margin: 8px 0;
}
</style> 